home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 March: Reference Library / Dev.CD Mar 96 RL / Dev.CD Mar 96 RL.toast / Technical Documentation / develop / develop Issue 25 / develop Issue 25 code / Display Manager Sample / Source / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-19  |  4.2 KB  |  196 lines  |  [TEXT/CWIE]

  1. /***********************************************************************
  2. #
  3. #        utils.c
  4. #
  5. #        This segment handles file parsing, and basic utility functions.
  6. #
  7. #        Author: Michael Marinkovich
  8. #                Apple Developer Technical Support
  9. #
  10. #
  11. #        Modification History: 
  12. #
  13. #            6/4/95        MWM     Initial coding                     
  14. #            10/12/95    MWM        cleaned up
  15. #
  16. #        Copyright © 1992-95 Apple Computer, Inc., All Rights Reserved
  17. #
  18. #
  19. ***********************************************************************/
  20.  
  21. #include <Events.h>
  22. #include <ToolUtils.h>
  23. #include <Gestalt.h>
  24. #include <OSUtils.h>
  25. #include <StandardFile.h>
  26.  
  27. #include "App.h"
  28. #include "Proto.h"
  29.  
  30.  
  31. //----------------------------------------------------------------------
  32. //
  33. //    DoOpenNew - query user for new file of type 'PICT'. Open it and 
  34. //                display in a new window.
  35. //
  36. //----------------------------------------------------------------------
  37.  
  38. void DoOpenNew(void)
  39. {
  40.     OSErr                    err;
  41.     PicHandle                pict;
  42.     StandardFileReply        reply;
  43.     SFTypeList                 typeList;
  44.     OSType                    type = 'PICT';
  45.     WindowRef                window;
  46.     Rect                    bounds;
  47.     Rect                    screenRect;
  48.     short                    numTypes;
  49.     DocHnd                    doc;
  50.  
  51.     numTypes = 1;
  52.     typeList[0] = type;
  53.     StandardGetFile(nil, numTypes, (unsigned long *)&typeList, &reply);
  54.     if ( reply.sfGood ){
  55.         pict = ReadFile(reply.sfFile);
  56.         if (pict != nil) {
  57.             bounds = (**pict).picFrame;
  58.             ZeroRect(&bounds);
  59.             OffsetRect(&bounds, 2, GetMBarHeight() * 2);
  60.             bounds.right += kScrollWidth;
  61.             bounds.bottom += kScrollWidth;
  62.             screenRect = qd.screenBits.bounds;
  63.             if (bounds.right > screenRect.right) 
  64.                 bounds.right = screenRect.right;
  65.             if (bounds.bottom > screenRect.bottom) 
  66.                 bounds.bottom = screenRect.bottom;
  67.  
  68.             window = CreateWindow(nil, nil, &bounds, reply.sfFile.name, false,
  69.                                   zoomDocProc, kDocKind, (WindowPtr)-1, true, nil );
  70.  
  71.             if (window != nil) {
  72.                 doc = (DocHnd)GetWRefCon(window);
  73.                 if (doc != nil)
  74.                     (**doc).world = PictToWorld(pict, &err);
  75.                     if ((**doc).world == nil || err != noErr) {
  76.                         KillPicture(pict);
  77.                         RemoveWindow(window);
  78.                         HandleError(err, false);
  79.                     }
  80.                     else
  81.                         AdjustScrollbars(window,true);
  82.  
  83.             }
  84.             if (pict != nil)
  85.                 KillPicture(pict);
  86.         }                              
  87.     }
  88.     
  89. }
  90.  
  91.  
  92. //----------------------------------------------------------------------
  93. //
  94. //    ReadFile - open and read disk data, returning PICT.
  95. //                
  96. //
  97. //----------------------------------------------------------------------
  98.  
  99. PicHandle ReadFile(FSSpec spec)
  100. {
  101.     OSErr                    err;
  102.     PicHandle                pict;
  103.     long                    pictSize;
  104.     long                    fileSize;
  105.     short                    refNum;
  106.  
  107.     SetCursor(*GetCursor(watchCursor));            // set the cursor to a watch while busy
  108.     
  109.     err = FSpOpenDF( &spec, fsRdWrShPerm, &refNum );
  110.     if ( err == noErr ) {
  111.         err = GetEOF( refNum, &fileSize );
  112.         if ( err == noErr ) {
  113.             err = SetFPos(refNum, fsFromMark, 512);   // set position to our picture data
  114.             if ( err == noErr ) {
  115.                 pictSize = fileSize - 512;
  116.                 pict = (PicHandle)NewHandle(pictSize);
  117.                 err = MemError();
  118.  
  119.                 if ( err == noErr && pict != nil ) {
  120.                     HLock((Handle) pict);
  121.                     err = FSRead(refNum, &pictSize, *pict);        // read in the pict data
  122.                     HUnlock((Handle) pict);
  123.                 }
  124.             }
  125.         }
  126.     
  127.         FSClose( refNum );            // close the file
  128.         SetCursor( &qd.arrow );        // set cursor back to arrow
  129.     }
  130.     
  131.     if (err != noErr)
  132.         pict = nil;
  133.     
  134.     return pict;
  135.  
  136. }
  137.     
  138.  
  139. //----------------------------------------------------------------------
  140. //
  141. //    ZeroRect - zero the top-left points of a rect
  142. //                
  143. //
  144. //----------------------------------------------------------------------
  145.  
  146. void ZeroRect(Rect *r)
  147. {
  148.     Rect        zr;
  149.     
  150.     zr = *r;
  151.     
  152.     if (zr.left < 0)
  153.         OffsetRect(&zr,zr.left,0);
  154.     if (zr.top < 0)
  155.         OffsetRect(&zr,0,zr.top);
  156.     
  157.     OffsetRect(&zr,-zr.left,-zr.top);
  158.     
  159.     *r = zr;
  160.  
  161.  
  162.  
  163. //----------------------------------------------------------------------
  164. //
  165. //    pstrcpy - pascal string copy
  166. //                
  167. //
  168. //----------------------------------------------------------------------
  169.  
  170. void pstrcpy(StringPtr dst, StringPtr src)
  171. {
  172.     short    c;
  173.     
  174.     for (c = *src; c > -1; c--)
  175.         dst[c] = src[c];
  176. }
  177.  
  178.  
  179. //----------------------------------------------------------------------
  180. //
  181. //    pstrcat - pascal string concat
  182. //                
  183. //
  184. //----------------------------------------------------------------------
  185.  
  186. void pstrcat(StringPtr dst, StringPtr src)
  187. {
  188.     
  189.     short    c;
  190.  
  191.     for (c = 1; c < src[0] + 1; c++)
  192.         dst[dst[0] + c] = src[c];
  193.     dst[0] += src[0];
  194. }
  195.